package com.ruoyi.area.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.area.mapper.AreaMapper;
import com.ruoyi.common.core.domain.entity.Area;
import com.ruoyi.area.service.IAreaService;

import javax.annotation.Resource;

/**
 * 区域概览Service业务层处理
 * 
 * @author yexiao
 * @date 2024-06-15
 */
@Service
public class AreaServiceImpl implements IAreaService 
{
    @Resource
    private AreaMapper areaMapper;

    /**
     * 查询区域概览
     * 
     * @param areaId 区域概览主键
     * @return 区域概览
     */
    @Override
    public Area selectAreaByAreaId(Long areaId)
    {
        return areaMapper.selectAreaByAreaId(areaId);
    }

    /**
     * 查询区域概览列表
     * 
     * @param area 区域概览
     * @return 区域概览
     */
    @Override
    public List<Area> selectAreaList(Area area)
    {
        return areaMapper.selectAreaList(area);
    }

    /**
     * 新增区域概览
     * 
     * @param area 区域概览
     * @return 结果
     */
    @Override
    public int insertArea(Area area)
    {
        return areaMapper.insertArea(area);
    }

    /**
     * 修改区域概览
     * 
     * @param area 区域概览
     * @return 结果
     */
    @Override
    public int updateArea(Area area)
    {
        return areaMapper.updateArea(area);
    }

    /**
     * 批量删除区域概览
     * 
     * @param areaIds 需要删除的区域概览主键
     * @return 结果
     */
    @Override
    public int deleteAreaByAreaIds(Long[] areaIds)
    {
        return areaMapper.deleteAreaByAreaIds(areaIds);
    }

    /**
     * 删除区域概览信息
     * 
     * @param areaId 区域概览主键
     * @return 结果
     */
    @Override
    public int deleteAreaByAreaId(Long areaId)
    {
        return areaMapper.deleteAreaByAreaId(areaId);
    }

    @Override
    public List<TreeSelect> selectAreaTreeList(Area area) {
        // 获取所有区域列表
        List<Area> areas = selectAreaList(area);
        // 传递区域列表，构建区域树
        return buildAreaTreeSelect(areas);
    }

    @Override
    public List<TreeSelect> buildAreaTreeSelect(List<Area> areas) {
        List<Area> areaTrees = buildAreaTree(areas);
        return areaTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<Area> buildAreaTree(List<Area> areas) {
        // 获取除了顶级节点外的所有节点
        List<Area> returnList = new ArrayList<>();
        // 将除了顶级节点的所有节点存入temp
        List<Long> temp = areas.stream().filter(obj -> obj.getParentId() != 0).map(Area::getAreaId).collect(Collectors.toList());
        //遍历每个节点
        for(Area area : areas){
            //如果是顶级节点,遍历该节点所有子节点
            if(!temp.contains(area.getAreaId())){
                recursionFn(areas,area);
                returnList.add(area);
            }
        }
        if(returnList.isEmpty()){
            returnList = areas;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<Area> list, Area t)
    {
        // 得到子节点列表
        List<Area> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Area tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Area> getChildList(List<Area> list, Area t)
    {
        List<Area> tlist = new ArrayList<Area>();
        Iterator<Area> it = list.iterator();
        while (it.hasNext())
        {
            Area n = (Area) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getAreaId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Area> list, Area t)
    {
        return getChildList(list, t).size() > 0;
    }
}
